package com.putao.album.main.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.putao.album.util.Loger;

/**
 * Created by jidongdong on 15/6/16.
 */
public class FlipRotateImageView extends ImageView {
    float down_x;
    boolean mScrollToLeft = false;
    private OnAnimationEndListener mOnAnimationEndListener;
    private FlipRotateImageView mView;

    public FlipRotateImageView(Context context) {
        super(context);
        init();
    }

    public FlipRotateImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FlipRotateImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    void init() {
        mView = this;
    }

    /**
     * 设置动画监听
     *
     * @param listener
     */
    public void setOnAnimationEndListener(OnAnimationEndListener listener) {
        this.mOnAnimationEndListener = listener;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down_x = event.getX();
                doScaleAnimation(true);
                break;
            case MotionEvent.ACTION_UP:
                float delta = (event.getX() - down_x);
                if (Math.abs(delta) > 10) {
                    mScrollToLeft = delta < 0;
                    doRotateAnimation();
                } else {
                    doScaleAnimation(false);
                }
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path clipPath = new Path();
        float w = this.getWidth();
        float h = this.getHeight();
        Loger.d("w:" + w + ",h:" + h);
        clipPath.addRoundRect(new RectF(0f, 0f, w, h), 10.0f, 10.0f, Path.Direction.CW);
        canvas.clipPath(clipPath);
        super.onDraw(canvas);
    }

    void doScaleAnimation(boolean down) {
        if (down) {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(this, "scaleX", 1.0f, 0.9f),
                    ObjectAnimator.ofFloat(this, "scaleY", 1.0f, 0.9f));
            set.setDuration(200);
            set.start();
        } else {
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(this, "scaleX", 0.9f, 1.0f),
                    ObjectAnimator.ofFloat(this, "scaleY", 0.9f, 1.0f));
            set.setDuration(200);
            set.start();
        }
    }

    /**
     * 播放动画
     */
    void doRotateAnimation() {
        if (mScrollToLeft) {
            setPivotX(0);
            setPivotY(-getLeft());
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(this, "rotation", 0, 90));
            set.setDuration(500);
            set.addListener(getAnimatorListener());
            set.start();
        } else {
            setPivotX(getRight());
            setPivotY(-getRight());
            AnimatorSet set = new AnimatorSet();
            set.playTogether(ObjectAnimator.ofFloat(this, "rotation", 0, -90));
            set.setDuration(500);
            set.addListener(getAnimatorListener());
            set.start();
        }
    }

    public interface OnAnimationEndListener {
        void onAnimationEnd(View v);
    }


    private Animator.AnimatorListener getAnimatorListener() {
        return new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (mOnAnimationEndListener != null) {
                    mOnAnimationEndListener.onAnimationEnd(mView);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        };
    }
}
